package com.cyx.gametool.controller;

import com.cyx.gametool.cons.MenuTabFxml;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Pane;

import java.io.IOException;
import java.net.URL;

public class GameToolController {

    @FXML
    public CheckMenuItem menuItem1_1;

    @FXML
    public CheckMenuItem menuItem1_2;

    @FXML
    public CheckMenuItem menuItem1_3;

    @FXML
    public CheckMenuItem menuItem1_4;

    @FXML
    public CheckMenuItem menuItem1_5;

    @FXML
    public MenuItem menuItem2_1;

    @FXML
    public MenuItem menuItem2_2;

    @FXML
    public MenuItem menuItem2_3;


    @FXML
    public TabPane tabPane;

    public void initialize() {
        // 设置TabPane的Tab可以拖拽策略
        tabPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER);

//        addTab("代码新增", "/fxml/chess/function/chess-function-view.fxml");
    }

    /**
     * 添加Tab
     * <p>
     * 例如：addTab("代码新增", "/fxml/chess/function/chess-function-view.fxml");
     *
     * @param tabName  Tab名称
     * @param fxmlName FXML文件路径
     */
    public void addTab(String tabName, String fxmlName) {
        String fxmlPath = "/fxml/tab/" + fxmlName;
        System.out.println("FXML路径: " + fxmlPath);
        URL fxmlUrl = getClass().getResource(fxmlPath);
        if (fxmlUrl == null) {
            System.out.println("FXML文件不存在: " + fxmlPath);
            return;
        } else {
            System.out.println("FXML文件存在: " + fxmlUrl);
        }


        // 使用FXMLLoader加载FXML文件
        try {
            FXMLLoader loader = new FXMLLoader(fxmlUrl);
            Pane content = loader.load();
            // 创建第一个Tab
            Tab tab = new Tab(tabName, content);
            // 将Tab添加到TabPane
            tabPane.getTabs().add(tab);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 移除Tab
     *
     * @param tabName Tab名称
     */
    public void removeTab(String tabName) {
        tabPane.getTabs().removeIf(tab -> tab.getText().equals(tabName));
    }

    /**
     * checkMenuItem菜单的点击事件
     *
     * @param actionEvent 事件对象
     */
    public void checkMenuItemOnAction(ActionEvent actionEvent) {
        // 获取点击的菜单选项
        CheckMenuItem checkMenuItem = (CheckMenuItem) actionEvent.getSource();
        boolean selected = checkMenuItem.isSelected();
        String text = checkMenuItem.getText();
        MenuTabFxml byName = MenuTabFxml.getByName(text);
        if (byName == null) {
            System.out.printf("%s 选择的菜单没有对应的Tab：%s\n", text, selected);
            return;
        }
        if (selected) {
            addTab(byName.getName(), byName.getFxml());
        } else {
            removeTab(byName.getName());
        }
        System.out.printf("%s 被选中了：%s\n", text, selected);
    }
}
